package com.geofeature;

import java.util.ArrayList;
import java.util.List;

import com.geofeature.models.Task;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class GeoFeature extends Activity {
    /**
     * Called when the activity is first created.
     */
    private TasksListAdapter tasksListAdapter;
    List<Task> tasks;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);

        // Init button for adding new task
        Button addNewTaskButton = (Button) findViewById(R.id.add_task_btn);
        addNewTaskButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                addTask();
            }
        });


        tasks = getRealTasksFromDb();
        tasksListAdapter = new TasksListAdapter(getBaseContext(), R.layout.tasks_list_item, tasks);
        tasksListAdapter.setNotifyOnChange(true);

        ListView tasksListView = (ListView) findViewById(R.id.tasks_list);
        tasksListView.setAdapter(tasksListAdapter);


        tasksListView.setOnItemClickListener(new OnItemClickListener() {

            //@Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                    long arg3) {
                Task task = tasksListAdapter.getItem(position);
                Intent toTaskActivity = new Intent(GeoFeature.this, TaskActivity.class);
                toTaskActivity.putExtra("task", task);
                startActivity(toTaskActivity);
                //Toast.makeText(getApplicationContext(), "Delete task: " + task.id + " :: " + task.name, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private List<Task> getRealTasksFromDb() {
        GeoDbAdapter db = new GeoDbAdapter(this);
        db.open();
        List<Task> tasks = db.fetchAllTasks();
        db.close();
        return tasks;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.add_task:
                addTask();
                return true;
            case R.id.debug_add_task:
                Intent debAddTAsk = new Intent(GeoFeature.this, TaskActivity.class);
                startActivity(debAddTAsk);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void addTask() {
        Intent intent = new Intent(GeoFeature.this, GeoMap.class);
        startActivity(intent);
    }

    private List<Task> genFakeTaskList() {
        // Init tasks list
        List<Task> tasks = new ArrayList<Task>();
        for (int i = 0; i < 5; i++) {
            Task task = new Task();
            task.id = i;
            task.name = "Task name #" + i;
            tasks.add(task);
        }
        return tasks;
    }
}